package com.stu.chart.controller;

import com.stu.chart.entity.Staff;
import com.stu.chart.security.Md5PasswordEncoder;
import com.stu.chart.service.StaffService;
import com.stu.chart.service.WebSocketServer;
import com.stu.chart.util.Rvo;
import com.stu.chart.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import javax.websocket.Session;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author dsj
 */
@RestController
public class ChatController {

	@Autowired
	StaffService loginservice;
	@Autowired
	private WebSocketServer webSocketServer;

	@RequestMapping("/onlineusers")
	public   List<Map<String,String>> onlineusers(@RequestParam("currentuser") String currentuser) {
		ConcurrentHashMap<String, Session> map = WebSocketServer.getSessionPools();
		Set<String> set = map.keySet();
		Iterator<String> it = set.iterator();
        List<Map<String,String>> r = new ArrayList<>();
		while (it.hasNext()) {
			String entry = it.next();
			Staff user = loginservice.getUser(entry);
			Map<String,String> on = new HashMap<>();
			if (!entry.equals(currentuser)) {
				on.put("username",entry);
				on.put("nick",user.getNick());
				on.put("picture",user.getPicture());
				r.add(on);
			}
		}
		return r;
	}


	@RequestMapping("getuid")
	public Staff getuid(@RequestParam("username") String username) {
		return loginservice.getUser(username);
	}

	@RequestMapping("forbid")
	public Rvo forbid(@RequestParam String staffId){
		Staff user1 = loginservice.getById(staffId);
		user1.setStatus(1);
		loginservice.updateById(user1);
		return Rvo.ok();
	}

	@RequestMapping("unForbid")
	public Rvo unForbid(@RequestParam String staffId){
		Staff user1 = loginservice.getById(staffId);
		user1.setStatus(0);
		loginservice.updateById(user1);
		return Rvo.ok();
	}

	@RequestMapping(value = "/currentuser", method = RequestMethod.GET)
	public Staff currentuser() {
		User user = SecurityUtil.getUser();
		return loginservice.getUser(user.getUsername());
	}

	@RequestMapping(value = "/myInfo", method = RequestMethod.GET)
	public Rvo myInfo() {
		User user = SecurityUtil.getUser();
		return Rvo.ok(loginservice.getUser(user.getUsername()));
	}

	@RequestMapping(value = "/searchStaff", method = RequestMethod.GET)
	public Rvo searchStaff(String name,Integer age1,Integer age2,String sex){
		return Rvo.ok(loginservice.searchStaff(name,age1,sex,age2));
	}

	@RequestMapping("updateStaff")
	public Rvo updateStaff(@RequestBody Staff staff){
		boolean b = loginservice.updateById(staff);
		return b?Rvo.ok(): Rvo.error(null);
	}
	@Autowired
	Md5PasswordEncoder md5PasswordEncoder;
	@RequestMapping("saveStaff")
	public Rvo saveStaff(@RequestBody Staff staff){
		Staff user = loginservice.getUser(staff.getUsername());
		if(user != null){
			return Rvo.error("账号已注册");
		}
		staff.setStatus(0);
		staff.setRole(0);
		staff.setPassword(md5PasswordEncoder.encode(staff.getPassword()));
		boolean b = loginservice.save(staff);
		return b?Rvo.ok(): Rvo.error(null);
	}

}
